package com.jy.appmarket.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.jy.appmarket.download.DownloadItem;
import com.jy.appmarket.download.DownloadUtils;
import com.jy.appmarket.statistics.event.installSuccessEvent;
import com.jy.appmarket.statistics.umeng.myUmengStaticstics;
import com.rad.statistics.android.StatisticsAgent;

public class InnerInstalledReceiver extends BroadcastReceiver {
	public static final String ACTION_INNERINSTALLED = "com.jy.appmarket.action.INNERINSTALLED";
	private static final String EXTRA_NAME = "extra_name";
	private static final String EXTRA_PKG = "extra_pkg";
	private static final String EXTRA_MD5 = "extra_md5";

	@Override
	public void onReceive(Context context, Intent intent) {
		if (context == null || intent == null || !ACTION_INNERINSTALLED.equals(intent.getAction())) {
			return;
		}
		String name = intent.getStringExtra(EXTRA_NAME);
		String pkg = intent.getStringExtra(EXTRA_PKG);
		String md5 = intent.getStringExtra(EXTRA_MD5);
		if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pkg) || TextUtils.isEmpty(md5)) {
			return;
		}

		if (DownloadUtils.removeInstalledApk(context, pkg, md5)) {
			Toast.makeText(context, "已为您删除" + name + "的安装包", Toast.LENGTH_SHORT).show();
		}

		myUmengStaticstics.installSuccess(context, pkg);

		DownloadItem di = DownloadUtils.getDownloadItem(context, pkg, md5);
		if (di != null) {
			StatisticsAgent.onEvent(context, new installSuccessEvent(di.getPkgName(), di.getSource(), di.getMd5()));
		}

	}
}
